/*
 * author: zulfikar
 */
package com.ebdesk.view.masterdata;

import com.ebdesk.controller.masterdata.UserController;
import com.ebdesk.model.masterdata.Setting;
import com.ebdesk.model.masterdata.User;
import com.ebdesk.view.MainFrame;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.TableModel;

public class UserManagementInternalFrame extends javax.swing.JInternalFrame {
    private int totalRows = 0;
    private int pageNumber = 1;
    private int totalPage = 1;
    private int rowsPerPage = 5;
    private MainFrame mainFrame;
    
    private UserController userController;
    private TableModel tableModel;
    private boolean isSearching;
    
    private String[] modelRows;
    /**
     * Creates new form UserManagementInternalFrame
     */
    public UserManagementInternalFrame(MainFrame mainFrame) {
        userController = new UserController();
        try {
            tableModel = userController.getTableModelAllUser(pageNumber, rowsPerPage);
        } catch (SQLException ex) {
        }
        this.mainFrame = mainFrame;
        
        int[] rowsInt = new Setting().getRowsPerPageUser();
        modelRows = new String[rowsInt.length];
        for (int i = 0; i < rowsInt.length; i++) {
            modelRows[i] = rowsInt[i] + "";
        }
        
        try {
                        // Set System L&F
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                } 
                catch (UnsupportedLookAndFeelException e) {
                   // handle exception
                }
                catch (ClassNotFoundException e) {
                   // handle exception
                }
                catch (InstantiationException e) {
                   // handle exception
                }
                catch (IllegalAccessException e) {
                   // handle exception
                }
        
        initComponents();
        displayContent(tableModel);
       
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelTop = new javax.swing.JPanel();
        toolbarUser = new javax.swing.JToolBar();
        buttonAdd = new javax.swing.JButton();
        buttonDelete = new javax.swing.JButton();
        buttonEdit = new javax.swing.JButton();
        toolBarSearch = new javax.swing.JToolBar();
        comboSelect = new javax.swing.JComboBox();
        textSearch = new javax.swing.JTextField();
        buttonSearch = new javax.swing.JButton();
        panelBottom = new javax.swing.JPanel();
        toolBarControl = new javax.swing.JToolBar();
        comboRowPerPage = new javax.swing.JComboBox();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        buttonFirst = new javax.swing.JButton();
        buttonPrevious = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        labelStartPage = new javax.swing.JLabel();
        textNumberPage = new javax.swing.JTextField();
        labelNumberPage = new javax.swing.JLabel();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        buttonNext = new javax.swing.JButton();
        buttonLast = new javax.swing.JButton();
        jSeparator4 = new javax.swing.JToolBar.Separator();
        buttonRefresh = new javax.swing.JButton();
        jSeparator5 = new javax.swing.JToolBar.Separator();
        labelDisplay = new javax.swing.JLabel();
        panelContent = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableUser = new javax.swing.JTable();

        setTitle("User Management");

        toolbarUser.setRollover(true);

        buttonAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ebdesk/view/images/user_add.png"))); // NOI18N
        buttonAdd.setToolTipText("add new user");
        buttonAdd.setFocusable(false);
        buttonAdd.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonAdd.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonAdd.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                buttonAddMouseClicked(evt);
            }
        });
        toolbarUser.add(buttonAdd);

        buttonDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ebdesk/view/images/user_delete.png"))); // NOI18N
        buttonDelete.setToolTipText("delete user");
        buttonDelete.setFocusable(false);
        buttonDelete.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonDelete.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonDeleteActionPerformed(evt);
            }
        });
        toolbarUser.add(buttonDelete);

        buttonEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ebdesk/view/images/user_edit.png"))); // NOI18N
        buttonEdit.setToolTipText("edit user");
        buttonEdit.setFocusable(false);
        buttonEdit.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonEdit.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonEdit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                buttonEditMouseClicked(evt);
            }
        });
        toolbarUser.add(buttonEdit);

        toolBarSearch.setRollover(true);

        comboSelect.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Username", "Name", "Email" }));
        toolBarSearch.add(comboSelect);

        textSearch.setToolTipText("Search");
        textSearch.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                textSearchKeyPressed(evt);
            }
        });
        toolBarSearch.add(textSearch);

        buttonSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ebdesk/view/images/search.gif"))); // NOI18N
        buttonSearch.setFocusable(false);
        buttonSearch.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonSearch.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonSearch.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                buttonSearchMouseClicked(evt);
            }
        });
        toolBarSearch.add(buttonSearch);

        javax.swing.GroupLayout panelTopLayout = new javax.swing.GroupLayout(panelTop);
        panelTop.setLayout(panelTopLayout);
        panelTopLayout.setHorizontalGroup(
            panelTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelTopLayout.createSequentialGroup()
                .addComponent(toolbarUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 330, Short.MAX_VALUE)
                .addComponent(toolBarSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 405, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        panelTopLayout.setVerticalGroup(
            panelTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelTopLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelTopLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(toolBarSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(toolbarUser, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        toolBarControl.setRollover(true);

        comboRowPerPage.setModel(new javax.swing.DefaultComboBoxModel(this.modelRows));
        toolBarControl.add(comboRowPerPage);
        toolBarControl.add(jSeparator1);

        buttonFirst.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ebdesk/view/images/control_start.png"))); // NOI18N
        buttonFirst.setToolTipText("first");
        buttonFirst.setFocusable(false);
        buttonFirst.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonFirst.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonFirst.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonFirstActionPerformed(evt);
            }
        });
        toolBarControl.add(buttonFirst);

        buttonPrevious.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ebdesk/view/images/control_rewind.png"))); // NOI18N
        buttonPrevious.setToolTipText("previous");
        buttonPrevious.setFocusable(false);
        buttonPrevious.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonPrevious.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonPrevious.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonPreviousActionPerformed(evt);
            }
        });
        toolBarControl.add(buttonPrevious);
        toolBarControl.add(jSeparator2);

        labelStartPage.setText("Page ");
        toolBarControl.add(labelStartPage);

        textNumberPage.setEditable(false);
        toolBarControl.add(textNumberPage);

        labelNumberPage.setText(" of 1000");
        toolBarControl.add(labelNumberPage);
        toolBarControl.add(jSeparator3);

        buttonNext.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ebdesk/view/images/control_fastforward.png"))); // NOI18N
        buttonNext.setToolTipText("next");
        buttonNext.setFocusable(false);
        buttonNext.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonNext.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonNextActionPerformed(evt);
            }
        });
        toolBarControl.add(buttonNext);

        buttonLast.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ebdesk/view/images/control_end.png"))); // NOI18N
        buttonLast.setToolTipText("last");
        buttonLast.setFocusable(false);
        buttonLast.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonLast.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonLast.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonLastActionPerformed(evt);
            }
        });
        toolBarControl.add(buttonLast);
        toolBarControl.add(jSeparator4);

        buttonRefresh.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/ebdesk/view/images/table_refresh.png"))); // NOI18N
        buttonRefresh.setToolTipText("refresh");
        buttonRefresh.setFocusable(false);
        buttonRefresh.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonRefresh.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonRefresh.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                buttonRefreshMouseClicked(evt);
            }
        });
        toolBarControl.add(buttonRefresh);
        toolBarControl.add(jSeparator5);

        labelDisplay.setText("Display 1 to 15 of 123 Users");
        toolBarControl.add(labelDisplay);

        javax.swing.GroupLayout panelBottomLayout = new javax.swing.GroupLayout(panelBottom);
        panelBottom.setLayout(panelBottomLayout);
        panelBottomLayout.setHorizontalGroup(
            panelBottomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelBottomLayout.createSequentialGroup()
                .addComponent(toolBarControl, javax.swing.GroupLayout.PREFERRED_SIZE, 453, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(374, Short.MAX_VALUE))
        );
        panelBottomLayout.setVerticalGroup(
            panelBottomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(toolBarControl, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        panelContent.setBackground(new java.awt.Color(255, 255, 255));
        panelContent.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        tableUser.setAutoCreateRowSorter(true);
        tableUser.setModel(tableModel);
        tableUser.setDragEnabled(true);
        tableUser.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableUserMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tableUser);

        javax.swing.GroupLayout panelContentLayout = new javax.swing.GroupLayout(panelContent);
        panelContent.setLayout(panelContentLayout);
        panelContentLayout.setHorizontalGroup(
            panelContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 823, Short.MAX_VALUE)
        );
        panelContentLayout.setVerticalGroup(
            panelContentLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panelTop, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(panelBottom, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(panelContent, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(panelTop, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 276, Short.MAX_VALUE)
                .addComponent(panelBottom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addGap(52, 52, 52)
                    .addComponent(panelContent, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGap(47, 47, 47)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonAddMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_buttonAddMouseClicked
        DialogAddNewUser dialog = new DialogAddNewUser(this.mainFrame, true);
        dialog.setVisible(true);

        if (dialog.addSuccess) {
            try {
                displayContent(userController.getTableModelAllUser(getPageNumber(), getRowsPerPage()));
                buttonLast.doClick();
            } catch (SQLException ex) {
                Logger.getLogger(UserManagementInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }//GEN-LAST:event_buttonAddMouseClicked

    private void buttonDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDeleteActionPerformed
        if (tableUser.getSelectedRow() >= 0) {
            if ((tableUser.getValueAt(tableUser.getSelectedRow(), 0).toString()).equals(this.mainFrame.getSession().getUser().getUsername())) {
                JOptionPane.showMessageDialog(this, "you cant delete yourself!");
            }
            else {
                int confirm = JOptionPane.showConfirmDialog(this, "Are you sure to delete this user?", "Confirmation Delete User", JOptionPane.OK_CANCEL_OPTION);

                if (confirm == JOptionPane.YES_OPTION) {
                    try {
                        userController.getUserManagement().removeUser(tableUser.getValueAt(tableUser.getSelectedRow(), 0).toString());
                        displayContent(userController.getTableModelAllUser(getPageNumber(), getRowsPerPage()));
                    } catch (SQLException ex) {
                    }


                }
            }
        }
    }//GEN-LAST:event_buttonDeleteActionPerformed

    private void buttonEditMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_buttonEditMouseClicked
        if (tableUser.getSelectedRow() >= 0) {
            try {
                User user = userController.getUserManagement().getUser(tableUser.getValueAt(tableUser.getSelectedRow(), 0).toString());
                new DialogEditUser(this.mainFrame, true, user).setVisible(true);
                displayContent(userController.getTableModelAllUser(getPageNumber(), getRowsPerPage()));
            } catch (SQLException ex) {
            }
        }
    }//GEN-LAST:event_buttonEditMouseClicked

    private void textSearchKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textSearchKeyPressed
        if (evt.getKeyCode() == evt.VK_ENTER) {
            setPageNumber(1);
            searching();
        }
    }//GEN-LAST:event_textSearchKeyPressed

    private void buttonSearchMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_buttonSearchMouseClicked
        setPageNumber(1);
        searching();
    }//GEN-LAST:event_buttonSearchMouseClicked

    private void buttonFirstActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonFirstActionPerformed
        try {
            setPageNumber(1);
            displayContent(userController.getTableModelAllUser(getPageNumber(), getRowsPerPage()));
        } catch (SQLException ex) {
        }
    }//GEN-LAST:event_buttonFirstActionPerformed

    private void buttonPreviousActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonPreviousActionPerformed
        if (getPageNumber() > 1) {
            try {
                setPageNumber(getPageNumber() - 1);
                displayContent(userController.getTableModelAllUser(getPageNumber(), getRowsPerPage()));
            } catch (SQLException ex) {
            }
        }
    }//GEN-LAST:event_buttonPreviousActionPerformed

    private void buttonNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonNextActionPerformed
        if (getPageNumber() < getTotalPage()) {
            try {
                pageNumber += 1;
                displayContent(userController.getTableModelAllUser(getPageNumber(), getRowsPerPage()));
            } catch (SQLException ex) {
            }
        }
    }//GEN-LAST:event_buttonNextActionPerformed

    private void buttonLastActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonLastActionPerformed
        try {
            setPageNumber(getTotalPage());

            tableModel = userController.getTableModelAllUser(pageNumber, getRowsPerPage());

            displayContent(tableModel);
        } catch (SQLException ex) {
        }
    }//GEN-LAST:event_buttonLastActionPerformed

    private void buttonRefreshMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_buttonRefreshMouseClicked
        try {
            this.setPageNumber(1);
            tableModel = userController.getTableModelAllUser(pageNumber, getRowsPerPage());

            displayContent(tableModel);
        } catch (SQLException ex) {
        } catch (java.lang.ArrayIndexOutOfBoundsException e) {
        }
    }//GEN-LAST:event_buttonRefreshMouseClicked

    private void tableUserMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableUserMouseClicked
        if (evt.getClickCount() == 2) {
            try {
                String username = tableUser.getValueAt(tableUser.getSelectedRow(), 0).toString();

                new DialogDetailUser(this.mainFrame, true, userController.getUserManagement().getUser(username)).setVisible(true);
            } catch (SQLException ex) {
            }
        }
    }//GEN-LAST:event_tableUserMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonAdd;
    private javax.swing.JButton buttonDelete;
    private javax.swing.JButton buttonEdit;
    private javax.swing.JButton buttonFirst;
    private javax.swing.JButton buttonLast;
    private javax.swing.JButton buttonNext;
    private javax.swing.JButton buttonPrevious;
    private javax.swing.JButton buttonRefresh;
    private javax.swing.JButton buttonSearch;
    private javax.swing.JComboBox comboRowPerPage;
    private javax.swing.JComboBox comboSelect;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JToolBar.Separator jSeparator4;
    private javax.swing.JToolBar.Separator jSeparator5;
    private javax.swing.JLabel labelDisplay;
    private javax.swing.JLabel labelNumberPage;
    private javax.swing.JLabel labelStartPage;
    private javax.swing.JPanel panelBottom;
    private javax.swing.JPanel panelContent;
    private javax.swing.JPanel panelTop;
    private javax.swing.JTable tableUser;
    private javax.swing.JTextField textNumberPage;
    private javax.swing.JTextField textSearch;
    private javax.swing.JToolBar toolBarControl;
    private javax.swing.JToolBar toolBarSearch;
    private javax.swing.JToolBar toolbarUser;
    // End of variables declaration//GEN-END:variables

    private void searching() {
        isSearching = true;
        String mode = comboSelect.getSelectedItem().toString();
        String isi = textSearch.getText();

        if (mode.equalsIgnoreCase("Username")) {
                try {
                    displayContent(userController.getTableModelSearchingUserByUsername(isi, getPageNumber(), getRowsPerPage()));
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
        } else if (mode.equalsIgnoreCase("Name")) {
                try {
                    displayContent(userController.getTableModelSearchingUserByName(isi, getPageNumber(), getRowsPerPage()));
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
        } else if (mode.equalsIgnoreCase("email")) {
                try {
                    displayContent(userController.getTableModelSearchingUserByEmail(isi, getPageNumber(), getRowsPerPage()));
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
        }
    }
    
        private void displayContent(TableModel tableModel) {
		setRowsPerPage((int) Integer.valueOf(comboRowPerPage.getSelectedItem().toString()));
		setTotalRows(userController.getTotalRow());
                
		setTotalPage((int) Math.ceil((getTotalRows() * 1.0) / getRowsPerPage()));
                
		if (getPageNumber() == 1) {
			buttonFirst.setEnabled(false);
			buttonPrevious.setEnabled(false);
		} else {
			buttonFirst.setEnabled(true);
			buttonPrevious.setEnabled(true);
		}
 
		if (getPageNumber() == getTotalPage()) {
			buttonNext.setEnabled(false);
			buttonLast.setEnabled(false);
		} else {
			buttonNext.setEnabled(true);
			buttonLast.setEnabled(true);
		}
 
		textNumberPage.setText(String.valueOf(getPageNumber()));
		labelNumberPage.setText(" of " + getTotalPage() + " ");
		labelDisplay.setText("Display of  " + (int)Math.max((getPageNumber() - 1) * getTotalRows(), 1) + " to " + 
                        (int)Math.min((getPageNumber()-1) * getTotalRows() + getTotalRows(), this.getTotalRows()) + " of " + 
                        getTotalRows() + "  Users ");
                
                this.tableModel = tableModel;
		tableUser.setModel(tableModel);
	}

    /**
     * @return the totalRows
     */
    public int getTotalRows() {
        return totalRows;
    }

    /**
     * @param totalRows the totalRows to set
     */
    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
    }

    /**
     * @return the pageNumber
     */
    public int getPageNumber() {
        return pageNumber;
    }

    /**
     * @param pageNumber the pageNumber to set
     */
    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }

    /**
     * @return the totalPage
     */
    public int getTotalPage() {
        return totalPage;
    }

    /**
     * @param totalPage the totalPage to set
     */
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    /**
     * @return the rowsPerPage
     */
    public int getRowsPerPage() {
        return new Integer(this.comboRowPerPage.getSelectedItem().toString());
    }

    /**
     * @param rowsPerPage the rowsPerPage to set
     */
    public void setRowsPerPage(int rowsPerPage) {
        this.rowsPerPage = rowsPerPage;
    }
        
    /**
    * @param args the command line arguments
    */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                
//                    new UserManagementInternalFrame(new MainFrame()).setVisible(true);
//                
//            }
//        });
//    }   
}
